package com.iflytek.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iflytek.entity.Admin;
import com.iflytek.entity.User;
import com.iflytek.service.AdminService;
import com.iflytek.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private AdminService adminService;

    @GetMapping("/login")
    public String toLogin() {
        return "login";
    }

    @PostMapping("/login")
    public String doLogin(String username, String password,
                          HttpSession session) throws Exception {
        //TODO 和数据库中的用户名密码比对
        QueryWrapper<Admin> w = new QueryWrapper<>();
        w.eq("username", username);
        Admin admin = adminService.getOne(w);
        if (password == null) {
           throw new Exception("输入密码");
        } else if (admin.getPassword().equals(password) ) {
            //登录成功
            System.out.println("登录成功！！！！");
            //把用户的信息放入Session
            session.setAttribute("username", username);
            return "login";
        } else {
            System.out.println("登录失败！！！！");
            return "login";
        }

    }


}
